clear all
set more off
tempfile temp xxx

** PATHS
local ubigeo ../../ubigeo

** DATA
use `ubigeo'/ubigeo1993

sort coddpto codprov coddist
by coddpto: gen DEPTname = nombre[1]
by coddpto codprov: gen PROVname = nombre[1]
gen DISTname = nombre
replace DEPTname = trim(DEPTname)
replace PROVname = trim(PROVname)
replace DISTname = trim(DISTname)

drop if coddist == "00"
gen ubigeo = coddpto + codprov + coddist
drop coddpto codprov coddist nombre

replace DISTname = subinstr(DISTname,"¥","Ñ",.)
replace PROVname = subinstr(PROVname,"¥","Ñ",.)

gen y = _n
rename ubigeo ubigeo1993
save `temp', replace


use `ubigeo'/ubigeo2006, clear 
sort coddpto codprov coddist
by coddpto: gen DEPTname = nombre[1]
by coddpto codprov: gen PROVname = nombre[1]
gen DISTname = nombre
replace DEPTname = trim(DEPTname)
replace PROVname = trim(PROVname)
replace DISTname = trim(DISTname)

replace DISTname = subinstr(DISTname,"¥","Ñ",.)
replace PROVname = subinstr(PROVname,"¥","Ñ",.)

drop if coddist == "00"
gen ubigeo2006 = coddpto + codprov + coddist
drop coddpto codprov coddist nombre

keep ubigeo *name*
gen x = _n

reclink DEPTname PROVname DISTname using `temp', idmaster(x) idusing(y) gen(score)
tab _merge
destring ubigeo*, replace
gen ubigeohost = ubigeo2006
gsort -score ubigeohost
tab ubigeohost if _merge == 1

replace ubigeo1993 = 030202 if ubigeohost == 030219 & _merge == 1 // nuevo chimbote to old chimbote
replace ubigeo1993 = 030501 if ubigeohost == 030506 & _merge == 1 // challhuahuacho to tambobamba
replace ubigeo1993 = 030706 if ubigeohost == 030714 & _merge == 1 // curasco to micaela bastidas
replace ubigeo1993 = 040511 if ubigeohost == 040520 & _merge == 1 // majes to lluta
replace ubigeo1993 = 050101 if ubigeohost == 050115 & _merge == 1 // jesus nazareno to ayacucho
replace ubigeo1993 = 050407 if ubigeohost == 050408 & _merge == 1 // llochegua to sivia
replace ubigeo1993 = 060307 if ubigeohost == 060312 & _merge == 1 // la libertad de pallan to miguel iglesias
replace ubigeo1993 = 060413 if ubigeohost == 060419 & _merge == 1 // chalamarca to paccha
replace ubigeo1993 = 080806 if ubigeohost == 080808 & _merge == 1 // alto pichigua to pichigua
replace ubigeo1993 = 090101 if ubigeohost == 090118 & _merge == 1 // ascension to huancavelica
replace ubigeo1993 = 100102 if ubigeohost == 100111 & _merge == 1 // pillco arca to amarilis
replace ubigeo1993 = 100310 if ubigeohost == 101002 & _merge == 1 // banos to jivia
replace ubigeo1993 = 100302 if ubigeohost == 101104 & _merge == 1 // aparicio pomares to chupa
replace ubigeo1993 = 100306 if ubigeohost == 101108 & _merge == 1 // choras to chavinillo
replace ubigeo1993 = 130201 if ubigeohost == 130208 & _merge == 1 // casa grande to ascope
replace ubigeo1993 = 130609 if ubigeohost == 131103 & _merge == 1 // cormin
replace ubigeo1993 = 130112 if ubigeohost == 131202 & _merge == 1 // cguadalupito to old viru
replace ubigeo1993 = 130112 if ubigeohost == 131203 & _merge == 1 // viru to old viru
replace ubigeo1993 = 160401 if ubigeohost == 160404 & _merge == 1 // san pablo to ramon castill
replace ubigeo1993 = 160507 if ubigeohost == 160510 & _merge == 1 // jenaro herrera to saquena
replace ubigeo1993 = 160201 if ubigeohost == 160706 & _merge == 1 // andoas
replace ubigeo1993 = 170101 if ubigeohost == 170104 & _merge == 1 // laberinto to tambopata
replace ubigeo1993 = 170201 if ubigeohost == 170204 & _merge == 1 // huetetopue to manu
replace ubigeo1993 = 211201 if ubigeohost == 211209 & _merge == 1 // alto inambaro to sandia
replace ubigeo1993 = 211207 if ubigeohost == 211210 & _merge == 1 // putina to san juan
replace ubigeo1993 = 230101 if ubigeohost == 230110 // albarracin to tacna
replace ubigeo1993 = 240201 if ubigeohost == 240203 & _merge == 1 // albarracin to tacna
replace ubigeo1993 = 250301 if ubigeohost == 250303 & _merge == 1 // curimana to p abad


tab ubigeohost if ubigeo1993 == .

gen dd = DISTname == UDISTname
gen flag = (score < 0.7 & _merge == 3 & dd == 0)
tab ubigeohost if flag == 1

replace ubigeo1993 = 021801 if ubigeohost == 021809 & flag == 1 // nuevo chimbote to old chimbote
replace flag = 0 if ubigeohost == 021809 & flag == 1

replace ubigeo1993 = 030601 if ubigeohost == 030608 & flag == 1 // curimana to p abad
replace flag = 0 if ubigeohost == 030608 & flag == 1

replace ubigeo1993 = 080907 if ubigeohost == 080910 & flag == 1 // pichari to kimbiri
replace flag = 0 if ubigeohost == 080910 & flag == 1

replace flag = 0 if ubigeohost == 120906 & flag == 1 // san juan de iscos
replace flag = 0 if ubigeohost == 120908 & flag == 1 // tres de diciembre

replace ubigeo1993 = 160101 if ubigeohost == 160112 & flag == 1  // belen to iquitos
replace flag = 0 if ubigeohost == 160112 & flag == 1

replace ubigeo1993 = 160101 if ubigeohost == 160113 & flag == 1 // san juan bautista to iquitos
replace flag = 0 if ubigeohost == 160113 & flag == 1

replace ubigeo1993 = 160109 if ubigeohost == 160114 & flag == 1 // tentiente.. to putumayo
replace flag = 0 if ubigeohost == 160114 & flag == 1

replace ubigeo1993 = 210504 if ubigeohost == 210505 & flag == 1 // tentiente.. to putumayo
replace flag = 0 if ubigeohost == 210505 & flag == 1

replace flag = 0 if ubigeohost == 230402 & flag == 1 // albarracin to chucatami

replace ubigeo1993 = 250101 if ubigeohost == 250107 & flag == 1 // manantay to calleria
replace flag = 0 if ubigeohost == 250107 & flag == 1

replace ubigeo1993 = 250102 if ubigeohost == 250106 & flag == 1 // n requena to campo verde
replace flag = 0 if ubigeohost == 250106 & flag == 1

tab ubigeohost if flag == 1

keep ubigeo2006 ubigeo1993 
duplicates drop

** SAVE
save `ubigeo'/ubigeo2006to1993, replace

